Ext.define('Seq.app.DetailController', {
    extend: 'Seq.app.CardController',

    onRefreshData: function (view, record, params, opts) {
        var store = this.getStore(view.getBind().store.stub.name);
        if (params) {
            store.getProxy().extraParams = params;
        }
        store.reload();
    },

    /**
     * 合并记录到store中
     */
    merge: function (store, record) {
        var result = store.find('id', record.data.id, 0, false, true, true);
        if (-1 < result) { // 合并属性
            result = store.getAt(result);
            Ext.apply(result.data, record.data);
            result.commit();
        } else { // 新建记录
            result = store.add(record.data)[0];
        }
        return result;
    },

    /**
     * 复制一条记录的内容，用于编辑修改，，否则无法撤销修改
     */
    duplicate: function (record) {
        var duplicate = Ext.create(record.store.model, Ext.apply({}, record.data));
        duplicate.store = record.store;
        return duplicate;
    },

    onSave: function (button) {
        var me = this, form = me.getViewModel().get('currentView'), record = me.getViewModel().get('record');
        me.beforeSave && Ext.callback(me.beforeSave, me, [record]);
        Seq.Ajax.submit(form, {
            scope: me,
            fnSuccess: function (options, result) {
                var ret = me.merge(record.store, result);
                Ext.Msg.show({
                    title: '提示',
                    message: '保存成功',
                    width: 300,
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.INFO
                });
                me.afterSave && Ext.callback(me.afterSave, options.scope, [options, ret]);
            }
        }, record);
    },

    onOk: function (button) {
        var me = this, vm = me.getViewModel(), form = vm.get('currentView');
        var record = vm.get('record'), store = record.store, back = vm.get('back');
        me.beforeSave && Ext.callback(me.beforeSave, me, [record]);
        Seq.Ajax.submit(form, {
            scope: me,
            mask: form,
            fnSuccess: function (options, result) {
                Ext.apply(record.data, result.data);
                var ret = me.merge(store, record);
                me.afterSave && Ext.callback(me.afterSave, options.scope, [options, ret]);
                me.setCurrent(back);
            }
        }, record);
    },

    onCancel: function (button) {
        this.onBack(button);
    },

    onFirst: function (button) {
        var vm = this.getViewModel(), store = vm.get('record').store;
        vm.set('record', this.duplicate(store.first()));
    },

    onLast: function (button) {
        var vm = this.getViewModel(), store = vm.get('record').store;
        vm.set('record', this.duplicate(store.last()))
    },

    onPrevious: function (button) {
        var vm = this.getViewModel(), record = vm.get('record'), store = record.store;
        if (record.data.id) {
            var index = store.find('id', record.data.id, 0, false, true, true) - 1;
            -1 < index && vm.set('record', this.duplicate(store.getAt(index)));
        } else { // 新建时，id为空，直接显示最后一条记录
            vm.set('record', this.duplicate(store.last()));
        }
    },

    onNext: function (button) {
        var vm = this.getViewModel(), record = vm.get('record'), store = record.store;
        if (record.data.id) {
            var index = store.find('id', record.data.id, 0, false, true, true) + 1;
            index < store.count() && vm.set('record', this.duplicate(store.getAt(index)));
        }
    },

    onCopy: function (button) {
        var vm = this.getViewModel(), record = vm.get('record');
        var duplicate = this.duplicate(record);
        duplicate.data.id = null; // 新建记录，ID为null
        duplicate.data.name += ' - 副本';
        vm.set('record', duplicate);
    },

    showDetail: function (detail, back, record) {
        var me = this, vm = me.getViewModel();
        vm.set('record', this.duplicate(record));
        vm.set('back', back);
        me.setCurrent(detail);
    },

    onCellDblClick: function (view, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var grid = view.ownerCt, headerCt = grid.getHeaderContainer();
        var column = headerCt.getHeaderAtIndex(cellIndex);
        if (0 == cellIndex || 'checkcolumn' === column.xtype) { // 所有的check框，都不能产生双击效果
            return;
        }
        this.showDetail(grid.detail, grid, record);
    },

    onDeleteClick: function (button, e) {
        var me = this, grid = me.getViewModel().get('currentView'), records = grid.getSelection();
        Seq.Ajax.drop(records, {
            scope: me,
            afterDrop: me.afterDrop
        });
    },

    onEditActionClick: function (view, rowIndex, colIndex, item, e, record, row) {
        var grid = view.ownerCt;
        this.showDetail(grid.detail, grid, record);
    },

    onDeleteActionClick: function (view, rowIndex, colIndex, item, e, record, row) {
        var me = this, records = [];
        records.push(record);
        Seq.Ajax.drop(records, {
            scope: me,
            afterDrop: me.afterDrop
        });
    },

    onInsertClick: function (btn) {
        var me = this, grid = btn.up && btn.up('grid') || me.getView();
        var record = Ext.create(grid.getStore().model, {
            name: '新建' + grid.title
        });
        record.store = grid.getStore();
        me.beforeInsert && me.beforeInsert(record);
        me.showDetail(grid.detail, grid, record);
    }

});